package com.guigui.gulimall.order.listener;

import com.guigui.gulimall.order.entity.OrderEntity;
import com.guigui.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


@RabbitListener(queues = "order-release-order-queue")
@Service
public class OrderListener {

    @Autowired
    private OrderService orderService;

    /**
     * @param order 保存订单后，发送给延时队列${}的消息
     */
    @RabbitHandler
    public void orderHandler(OrderEntity order, Message message, Channel channel) throws IOException {
        try {
            // 询问是否要关闭订单
            orderService.closeOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }

}
